﻿
var ws;

function connectWB() {
    var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);

    if (support == null) {
        top.$.ligerDialog.warn("你的浏览器不支持websocket");
        return;
    }
    ws = new window[support]('ws://localhost:2012/');

    // when data is comming from the server, this metod is called
    ws.onmessage = function (evt) {
        if (evt.data === "close") {
        } else {
            var data = evt.data;
            data = eval("(" + data + ")");
            if (data.result === "success") {
                //显示身份证数据
                window.showIDCardData(data);
            }
        }
    };
    ws.onopen = function () {
        //readIDCard需要刷卡页面提供的刷卡参数
        if (window.readIDCard) {
            sendMessage("idcardReady");
        }
    };
    // when the connection is closed, this method is called
    ws.onclose = function () {
        var div = $("<div></div>");
        div.html = "请开启后台！";
        div.attr("id", "openHou");
        div.attr("style", "position:absolute; top:1px;");
        $("#openHou").remove();
        $("body").prepend(div);
//        $.ligerDialog.warn("请开启后台！");
    };
};

function sendMessage(value, obj, detail) {
    if (ws) {
        if (value === "print") {
            value = obj;
        }
        if (value === "design") {
            value = obj;
        }
        //通知读卡
        if (value === "idcardReady") {
            value = '{"Type": "idcard", "Action": "readCard"}';
        }
        if (obj) obj = eval("(" + obj + ")");
        determinePageOrNot(value, obj, detail);
    }

    function determinePageOrNot(value, obj, detail) {
        var page;
        var count = 5;//分页标准
        var usepage = false;
        if (obj) {
            if (obj.Value.length >= count || detail !== undefined) {
                usepage = true;
            }
        }
        if (usepage) {
            var master = "main";
            var url = obj.url;
            var housesid = obj.houseid;
            var servername = obj.servername;
            var step = obj.step;
            var action = obj.Action === "PrintData" ? "PrintDataSubpackage" : "PreviewDataSubpackage";
            if (detail) {
                master = "detail";
            }
            page = {
                Type: "print",
                Action: action,
                url: url,
                houseid: housesid,
                servername: servername,
                master: master,
                step: step,
                Index: 0,
                Value: ""
            };
            value = eval("(" + value + ")");
            var obj2 = value.Value;

            var i = 0;
            while (i < obj2.length) {
                page.Value = obj2.slice(i, count);
                if (obj2.length <= count) {
                    page.Index = 1;
                }

                ws.send(JSON.stringify(page));
                page.master = master + i;
                if (page.Index === 1 && page.master.indexOf("detail") !== -1) {
                    var objd = detail;
                    page = {
                        Type: "print",
                        Action: action,
                        url: url,
                        houseid: housesid,
                        servername: servername,
                        master: "enddetail",
                        step: step,
                        Index: 1,
                        Value: objd
                    };
                    ws.send(JSON.stringify(page));
                }

                i = i + 5;
                if (count + 5 >= obj2.length && page.Index === 0) {
                    count = obj2.length;
                    page.Index = 1;
                } else {
                    count = count + 5;
                }
            }
            return;
        } else {
            ws.send(value);
        }
    }
}